from django.shortcuts import render
from StudyPlatform.models import Courses, Grade
from django.contrib.auth.decorators import login_required


@login_required(login_url='login')
def searchCourses(request):
    searchContent = request.GET.get("searchContent")
    grades = Grade.objects.all()
    if searchContent != "":
        searchCoursesId = set(
            list(Courses.objects.filter(coursename__contains=searchContent).values_list("id", flat=True)) +
            list(Courses.objects.filter(videoname__contains=searchContent).values_list("id", flat=True))
        )
    else:
        searchCoursesId = []
    context = {
        "grades": grades,
        "searchCoursesId": searchCoursesId,
        "searchContent": searchContent,
    }
    return render(request, 'search_courses.html', context)
